<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 会员筛选器
 * @package  htdocs
 * @subpackage  module
 * @author   allen.qiang
 * @version 1.0
 */

class custSelector {

    /**
     * @var object 数据库实例
     */
    private static $dbca;

    /**
     * @var string 筛选器规则
     */
    private $selector_rule;

    /**
     * @var int 筛选器ID
     */
    private $selectID;

    /**
     * @var int 会员号
     */
    private $pcustID;

    public function __construct( &$dbca ) {
        self::$dbca = $dbca;
    }

    /**
     * 查询筛选器信息
     * @return bool
     */
    private function searchSelector() {

        $search_sql = 'SELECT * FROM cust_selector WHERE selectID = ?';
        $selector_array = self::$dbca->stmt_query($search_sql, 'i', $this->selectID);
        if (!$selector_array)
            return false;

        return true;

    }

    /**
     * 查询筛选器选择信息
     * @return mixed
     */
    private function searchSelectorDetail() {

        $search_sql = 'SELECT * FROM cust_selector_detail WHERE selectID = ?';
        $selector_detail_array = self::$dbca->stmt_query($search_sql, 'i', $this->selectID);
        if (!$selector_detail_array)
            return null;

        $item_array = null;

        foreach($selector_detail_array as &$item) {

            if (empty($item->selector))
                continue;
            $item_array[$item->selectType][] = $item;

            if ($item->ruleFLg) {
                $this->selector_rule = 'include';
            } else {
                $this->selector_rule = 'exclude';
            }

        }

        return $item_array;

    }

    /**
     * 检查会员是否符合会员筛选器
     * @param int $selectID 筛选器ID
     * @param int $pcustID 三级网购号
     * @return bool
     */
    public function isValid( $selectID, $pcustID ) {

        if (!isset($selectID) || !is_numeric($selectID))
            return true;

        if (!isset($pcustID) || !is_numeric($pcustID))
            return false;

        $this->selectID = $selectID;
        $this->pcustID = $pcustID;

        //查询筛选器信息
        if (!$this->searchSelector())
            return false;

        return $this->fitSelector();

    }

    /**
     * 检查会员匹配筛选器选项
     * @return bool
     */
    private function fitSelector() {

        try {

            //查询筛选器选择信息
            $selector_detail_array = $this->searchSelectorDetail();
            if (!$selector_detail_array)
                return false;

            $cust_obj = new entity('cust_master');
            $cust_obj->pcustID = $this->pcustID;
            if (!self::$dbca->fetch($cust_obj))
                return false;

            //符合数量
            $fit_num = 0;

            //检查商品是否符合筛选器选项
            foreach($selector_detail_array as $key => &$detail) {

                //是否在筛选器选项里
                $include = false;

                switch($key) {

                    case 1: //会员等级筛选

                        foreach ($detail as &$item) {
                            $rank_array = explode(',', $item->selector);
                            if (in_array($cust_obj->rankID, $rank_array)) {
                                $include = true;
                                break;
                            }
                        }

                        break;

                }

                if ($include) {
                    if ($this->selector_rule == 'exclude')
                        return false;
                    ++$fit_num;
                }

            }

            if ($this->selector_rule == 'include' && $fit_num != count($selector_detail_array))
                return false;

            return true;

        } catch (Exception $e) {
            error_log($e->getMessage());
            return false;
        }

    }

}